package com.bioroadlab.service;

import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.bioroadlab.dao.IndexFile;
import com.bioroadlab.pojo.Disease;
import com.bioroadlab.pojo.TestItem;
import com.bioroadlab.pojo.TestObject;

@Service
@Transactional
public class DiseaseService extends BaseService {
	private IndexFile indexFile;
	private Map<Disease, Map<String, Map<Set<TestObject>, Map<String, Set<TestItem>>>>> map;
	private Map<Disease, Map<String, Integer>> maprows3;
	private Map<Disease, Map<String, Integer>> maprows1;
	private Map<Disease, Map<String, Map<Set<TestObject>, Integer>>> maprows2;

	public List<Disease> findDiseaseByGaoji(String searchtype, String searchname) {
		return getDiseaseDao().findDiseaseByGaoji(searchtype, searchname);
	}

	public List<Disease> findByName(String diseasename) {
		return getDiseaseDao().findByName(diseasename);
	}

	public List<Disease> findByDiseaseKeywordName(String diseaseKeywordName) {
		List<Disease> diseaseList = getDiseaseDao().findByKeywordName(
				diseaseKeywordName);
		return diseaseList;
	}

	public Disease findBySn(int sn) {
		return getDiseaseDao().findById(sn);
	}

	public void updateDisease(Disease disease) {
		getDiseaseDao().updateDisease(disease);
	}

	public List<Disease> findAll() {
		List<Disease> diseaseList = getDiseaseDao().findAll();
		return diseaseList;
	}

	public List<Disease> getDiseaseList() {
		List<Disease> diseaseList = getDiseaseDao().getListDisease();
		return diseaseList;
	}

	public int indexFileForAnvencedSearchDiseaseAlias(String indexdir)
			throws IOException {
		return getIndexForAdvanced().indexFileForAnvencedSearchDiseaseAlias(
				indexdir);
	}

	public int indexFileForAnvencedSearchDiseaseName(String indexdir)
			throws IOException {
		return getIndexForAdvanced().indexFileForAnvencedSearchDiseaseName(
				indexdir);
	}

	public int indexFileForAnvencedSearchDiseaseKeyword(String indexdir)
			throws IOException {
		return getIndexForAdvanced().indexFileForAnvencedSearchDiseaseKeyword(
				indexdir);
	}

	public int indexFileForQuickSearch() throws IOException {
		return indexFile.indexFileForQuickSearch();
	}

	public List<Disease> findAllByPage(int nowPage) {
		return getDiseaseDao().findAllByPage(nowPage);

	}

	public IndexFile getIndexFile() {
		return indexFile;
	}

	@Autowired
	public void setIndexFile(IndexFile indexFile) {
		this.indexFile = indexFile;
	}

	public void exportDiseaseListOfClient(int[] checkDisease,
			List<Disease> diseaseList) {
		map = new LinkedHashMap<Disease, Map<String, Map<Set<TestObject>, Map<String, Set<TestItem>>>>>();
		maprows3 = new HashMap<Disease, Map<String, Integer>>();
		maprows1 = new HashMap<Disease, Map<String, Integer>>();
		maprows2 = new HashMap<Disease, Map<String, Map<Set<TestObject>, Integer>>>();
		List<Disease> dList = new ArrayList<Disease>();
		for (Integer i : checkDisease) {
			Disease disease = findBySn(i);
			if (!dList.contains(disease)) {
				dList.add(disease);
			}
		}
		diseaseList = dList;

		for (Disease disease : diseaseList) {
			if (!map.containsKey(disease)) {
				map.put(disease,
						new HashMap<String, Map<Set<TestObject>, Map<String, Set<TestItem>>>>());
				maprows3.put(disease, new HashMap<String, Integer>());
				maprows1.put(disease, new HashMap<String, Integer>());
				maprows2.put(
						disease,
						new HashMap<String, Map<Set<TestObject>, Integer>>());
			}
			Map<String, Map<Set<TestObject>, Map<String, Set<TestItem>>>> map0 = map
					.get(disease);
			Map<String, Integer> maprows01 = maprows1.get(disease);
			Map<String, Integer> maprows03 = maprows3.get(disease);
			Map<String, Map<Set<TestObject>, Integer>> maprows02 = maprows2
					.get(disease);
			for (TestItem testItem : disease.getTestItems()) {
				String test = testItem.getTest();
				if (!map0.containsKey(test)) {
					map0.put(
							test,
							new HashMap<Set<TestObject>, Map<String, Set<TestItem>>>());
					maprows03.put(disease.getName(), 0);
					maprows01.put(test, 0);
					maprows02.put(test,
							new HashMap<Set<TestObject>, Integer>());
				}
				Map<Set<TestObject>, Map<String, Set<TestItem>>> map1 = map0
						.get(test);
				Map<Set<TestObject>, Integer> maprow1 = maprows02.get(test);
				Set<TestObject> set = testItem.getTestObjects();
				if (!map1.containsKey(set)) {
					map1.put(set, new HashMap<String, Set<TestItem>>());
					maprow1.put(set, 0);
				}
				Map<String, Set<TestItem>> map2 = map1.get(set);
				String category = testItem.getTestCategory();
				if (!map2.containsKey(category)) {
					map2.put(category, new HashSet<TestItem>());
				}
				Set<TestItem> set2 = map2.get(category);
				set2.add(testItem);
				maprows03.put(disease.getName(),
						maprows03.get(disease.getName()) + 1);
				maprows01.put(test, maprows01.get(test) + 1);
				maprow1.put(set, maprow1.get(set) + 1);
			}
		}
	}

	public Map<Disease, Map<String, Map<Set<TestObject>, Map<String, Set<TestItem>>>>> getMap() {
		return map;
	}

	public void setMap(
			Map<Disease, Map<String, Map<Set<TestObject>, Map<String, Set<TestItem>>>>> map) {
		this.map = map;
	}

	public Map<Disease, Map<String, Integer>> getMaprows3() {
		return maprows3;
	}

	public void setMaprows3(Map<Disease, Map<String, Integer>> maprows3) {
		this.maprows3 = maprows3;
	}

	public Map<Disease, Map<String, Integer>> getMaprows1() {
		return maprows1;
	}

	public void setMaprows1(Map<Disease, Map<String, Integer>> maprows1) {
		this.maprows1 = maprows1;
	}

	public Map<Disease, Map<String, Map<Set<TestObject>, Integer>>> getMaprows2() {
		return maprows2;
	}

	public void setMaprows2(
			Map<Disease, Map<String, Map<Set<TestObject>, Integer>>> maprows2) {
		this.maprows2 = maprows2;
	}

}
